<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>singleton.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_singleton.js.html">singleton.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2009 Google Inc. All Rights Reserved
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This module simplifies testing code which uses many stateful
<a name="line17"></a> * singletons. The {@link goog.testing.singleton.addSingletonGetter} function
<a name="line18"></a> * adds a static {@code getInstance} method to the class, which in addition to
<a name="line19"></a> * creating and returning a singleton instance, registers it in a global
<a name="line20"></a> * repository. This way the effect of all {@code getInstance} calls can be
<a name="line21"></a> * simply reset in {@code tearDown} without knowing what singletons were
<a name="line22"></a> * instantiated in the tests.
<a name="line23"></a> *
<a name="line24"></a> * Usage:
<a name="line25"></a> * &lt;ol&gt;
<a name="line26"></a> *   &lt;li&gt;Load {@code base.js}.
<a name="line27"></a> *   &lt;li&gt;Load {@code goog.testing.singleton}. It overrides
<a name="line28"></a> *       {@link goog.addSingletonGetter} by
<a name="line29"></a> *       {@link goog.testing.addSingletonGetter}.
<a name="line30"></a> *   &lt;li&gt;Load the code to test.
<a name="line31"></a> *   &lt;li&gt;Call {@link goog.testing.singleton.reset} in the {@code tearDown}.
<a name="line32"></a> * &lt;/ol&gt;
<a name="line33"></a> *
<a name="line34"></a> */
<a name="line35"></a>
<a name="line36"></a>goog.provide(&#39;goog.testing.singleton&#39;);
<a name="line37"></a>
<a name="line38"></a>goog.require(&#39;goog.array&#39;);
<a name="line39"></a>
<a name="line40"></a>
<a name="line41"></a>/**
<a name="line42"></a> * List of all singleton classes for which the instance has been created.
<a name="line43"></a> * @type {Array.&lt;!Function&gt;}
<a name="line44"></a> * @private
<a name="line45"></a> */
<a name="line46"></a>goog.testing.singletons_ = [];
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * Adds a {@code getInstance} static method to the given class which in addition
<a name="line51"></a> * to always returning the same instance object, registers the constructor in a
<a name="line52"></a> * global array.
<a name="line53"></a> * @param {!Function} ctor The constructor for the class to add the static
<a name="line54"></a> *     method to.
<a name="line55"></a> */
<a name="line56"></a>goog.testing.singleton.addSingletonGetter = function(ctor) {
<a name="line57"></a>  ctor.getInstance = function() {
<a name="line58"></a>    if (!ctor.instance_) {
<a name="line59"></a>      ctor.instance_ = new ctor();
<a name="line60"></a>      goog.testing.singletons_.push(ctor);
<a name="line61"></a>    }
<a name="line62"></a>    return ctor.instance_;
<a name="line63"></a>  };
<a name="line64"></a>};
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Deletes all singleton instances, so {@code getInstance} will return a new
<a name="line69"></a> * instance on next call.
<a name="line70"></a> */
<a name="line71"></a>goog.testing.singleton.reset = function() {
<a name="line72"></a>  goog.array.forEach(goog.testing.singletons_, function(ctor) {
<a name="line73"></a>    delete ctor.instance_;
<a name="line74"></a>  });
<a name="line75"></a>  goog.array.clear(goog.testing.singletons_);
<a name="line76"></a>};
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * Overrides {@code goog.addSingletonGetter} in {@code base.js}.
<a name="line81"></a> */
<a name="line82"></a>goog.addSingletonGetter = goog.testing.singleton.addSingletonGetter;
</pre>


</body>
</html>
